package com.ruoyi.baselibrary.infobasespecialist.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.baselibrary.infobasecategory.service.IInfoBaseCategoryService;
import com.ruoyi.baselibrary.infobasespecialist.domain.InfoBaseSpecialist;
import com.ruoyi.baselibrary.infobasespecialist.service.IInfoBaseSpecialistService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专家信息Controller
 *
 * @author ruoyi
 * @date 2022-01-12
 */
@RestController
@RequestMapping("/infoBaseSpecialist/infoBaseSpecialist")
public class InfoBaseSpecialistController extends BaseController {
    @Autowired
    private IInfoBaseSpecialistService infoBaseSpecialistService;

    @Autowired
    private IInfoBaseCategoryService baseCategoryService;

    /**
     * 查询专家信息列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoBaseSpecialist infoBaseSpecialist) {
        startPage();
        /**
         * 该类别下的所有的子集
         */
        String baseCategoryByParentId = baseCategoryService.getBaseCategoryByParentId(infoBaseSpecialist.getCategory());
        infoBaseSpecialist.setCategoryIds(baseCategoryByParentId);
        infoBaseSpecialist.setCategory(null);
        List<InfoBaseSpecialist> list = infoBaseSpecialistService.selectInfoBaseSpecialistList(infoBaseSpecialist);
        for (InfoBaseSpecialist baseSpecialist : list) {
            baseSpecialist.setCategoryName(baseCategoryService.getBaseCategoryName(baseSpecialist.getCategory()));
        }
        return getDataTable(list);
    }

    /**
     * 导出专家信息列表
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:export')")
    @Log(title = "专家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoBaseSpecialist infoBaseSpecialist) {
        List<InfoBaseSpecialist> list = infoBaseSpecialistService.selectInfoBaseSpecialistList(infoBaseSpecialist);
        ExcelUtil<InfoBaseSpecialist> util = new ExcelUtil<InfoBaseSpecialist>(InfoBaseSpecialist.class);
        util.exportExcel(response, list, "专家信息数据");
    }

    /**
     * 获取专家信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(infoBaseSpecialistService.selectInfoBaseSpecialistById(id));
    }

    /**
     * 新增专家信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:add')")
    @Log(title = "专家信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoBaseSpecialist infoBaseSpecialist) {
        return toAjax(infoBaseSpecialistService.insertInfoBaseSpecialist(infoBaseSpecialist));
    }

    /**
     * 修改专家信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:edit')")
    @Log(title = "专家信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoBaseSpecialist infoBaseSpecialist) {
        return toAjax(infoBaseSpecialistService.updateInfoBaseSpecialist(infoBaseSpecialist));
    }

    /**
     * 删除专家信息
     */
    @PreAuthorize("@ss.hasPermi('infoBaseSpecialist:infoBaseSpecialist:remove')")
    @Log(title = "专家信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoBaseSpecialistService.deleteInfoBaseSpecialistByIds(ids));
    }
}
